<?php
class Ws{
	const HOST ="0.0.0.0";
	const PORT = 9501;
	public $ws = null;

	public function __construct(){
		$this->ws = new swoole_websocket_server('0.0.0.0',9501);
		$this->ws->on('open',[$this, 'onOpen']);
		$this->ws->on('message',[$this, 'onMessage']);
		$this->ws->on('close',[$this, 'onClose']);
		$this->ws->start();
	}

	/**
	 * 监听连接事件
	 * @Author   mdx
	 * @DateTime 2018-06-09
	 * @param    [type]     $ws      [description]
	 * @param    [type]     $request [description]
	 * @return   [type]              [description]
	 */
	public function onOpen($ws, $request){
		var_dump($request->id);
	}

	/**
	 * 监听消息事件
	 * @Author   mdx
	 * @DateTime 2018-06-09
	 * @param    [type]     $ws      [description]
	 * @param    [type]     $request [description]
	 * @return   [type]              [description]
	 */
	public function onMessage($ws, $frame){
		echo "serverOut:{$frame->data}\n"; 
		$ws->push($frame->fd,"server-push:".date('Y-m-d H:i:s'));
	}

	/**
	 * 监听关闭事件
	 * @Author   mdx
	 * @DateTime 2018-06-09
	 * @param    [type]     $ws      [description]
	 * @param    [type]     $request [description]
	 * @return   [type]              [description]
	 */
	public function onClose($ws, $fd){
		echo '客户端id:'.$fd.' 退出';
	}
}


$serv = new Ws();



